package com.ruoyi.business.workshop.dto;

import com.ruoyi.business.workshop.domain.WorkshopSlotInfo;
import com.ruoyi.business.workshop.domain.WorkshopPositionInfo;

import java.io.Serializable;
import java.util.List;

/**
 * 车间槽位数据传输对象
 * 包含槽位信息以及所属的工段、工区、厂房名称
 * 
 * @author ruoyi
 */
public class WorkshopSlotDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    // ========== 槽位基本信息 ==========
    /** 槽位编号 */
    private Integer slotNumber;
    
    /** 每槽点位数 */
    private Integer pointsPerSlot;
    
    /** 电源箱门开关状态 */
    private Boolean powerBoxDoorOpen;
    
    /** 槽控机门开关状态 */
    private Boolean controllerDoorOpen;
    
    /** 点位信息列表 */
    private List<WorkshopPositionInfo> pointInfos;

    // ========== 所属层级信息 ==========
    /** 所属工段名称 */
    private String sectionName;
    
    
    /** 所属厂房名称 */
    private String factoryName;

    /** 所属工区名称 */
    private String areaName;

    public WorkshopSlotDTO() {}

    public WorkshopSlotDTO(WorkshopSlotInfo slotInfo, String sectionName,  String factoryName,String areaName) {
        // 复制槽位基本信息
        this.slotNumber = slotInfo.getSlotNumber();
        this.pointsPerSlot = slotInfo.getPointsPerSlot();
        this.powerBoxDoorOpen = slotInfo.getPowerBoxDoorOpen();
        this.controllerDoorOpen = slotInfo.getControllerDoorOpen();
        this.pointInfos = slotInfo.getPointInfos();
        
        // 设置层级信息
        this.sectionName = sectionName;
        this.areaName = areaName;
        this.factoryName = factoryName;
    }

    // ========== 槽位基本信息 getter/setter ==========
    public Integer getSlotNumber() {
        return slotNumber;
    }

    public void setSlotNumber(Integer slotNumber) {
        this.slotNumber = slotNumber;
    }

    public Integer getPointsPerSlot() {
        return pointsPerSlot;
    }

    public void setPointsPerSlot(Integer pointsPerSlot) {
        this.pointsPerSlot = pointsPerSlot;
    }

    public Boolean getPowerBoxDoorOpen() {
        return powerBoxDoorOpen;
    }

    public void setPowerBoxDoorOpen(Boolean powerBoxDoorOpen) {
        this.powerBoxDoorOpen = powerBoxDoorOpen;
    }

    public Boolean getControllerDoorOpen() {
        return controllerDoorOpen;
    }

    public void setControllerDoorOpen(Boolean controllerDoorOpen) {
        this.controllerDoorOpen = controllerDoorOpen;
    }

    public List<WorkshopPositionInfo> getPointInfos() {
        return pointInfos;
    }

    public void setPointInfos(List<WorkshopPositionInfo> pointInfos) {
        this.pointInfos = pointInfos;
    }

    // ========== 层级信息 getter/setter ==========
    public String getSectionName() {
        return sectionName;
    }

    public void setSectionName(String sectionName) {
        this.sectionName = sectionName;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getFactoryName() {
        return factoryName;
    }

    public void setFactoryName(String factoryName) {
        this.factoryName = factoryName;
    }

    // ========== 业务方法 ==========
    
    /**
     * 判断槽位是否有异常（门状态异常）
     */
    public boolean hasError() {
        return Boolean.TRUE.equals(powerBoxDoorOpen) || Boolean.TRUE.equals(controllerDoorOpen);
    }

    /**
     * 获取下料点位数量
     */
    public int getDischargePointCount() {
        if (pointInfos == null) {
            return 0;
        }
        return (int) pointInfos.stream()
                .filter(point -> Boolean.TRUE.equals(point.getDischargeStatus()))
                .count();
    }

    /**
     * 获取卡堵点位数量
     */
    public int getJamPointCount() {
        if (pointInfos == null) {
            return 0;
        }
        return (int) pointInfos.stream()
                .filter(point -> Boolean.TRUE.equals(point.getJamStatus()))
                .count();
    }

    /**
     * 获取完整路径描述
     */
    public String getFullPath() {
        return String.format("%s > %s > %s > 槽位%d", areaName, factoryName, sectionName, slotNumber);
    }

    /**
     * 获取状态描述
     */
    public String getStatusDescription() {
        if (hasError()) {
            return "异常";
        }
        return "正常";
    }

    @Override
    public String toString() {
        return "WorkshopSlotDTO{" +
                "slotNumber=" + slotNumber +
                ", sectionName='" + sectionName + '\'' +
                ", areaName='" + areaName + '\'' +
                ", factoryName='" + factoryName + '\'' +
                ", powerBoxDoorOpen=" + powerBoxDoorOpen +
                ", controllerDoorOpen=" + controllerDoorOpen +
                ", dischargePoints=" + getDischargePointCount() +
                ", jamPoints=" + getJamPointCount() +
                '}';
    }
}
